package com.congee02;

public class FindCircleNum_NoCast {

    public int findCircleNum(int[][] isConnected) {

        int n = isConnected.length;

        boolean[] visited = new boolean[n];
        int ans = 0;
        for (int i = 0 ; i < n ; i ++ ) {

            if (! visited[i]) {
                ans ++;
                dfsHelp(isConnected, visited, i);
            }

        }

        return ans;
    }

    private void dfsHelp(int[][] isConnected, boolean[] visited, int cur) {

        visited[cur] = true;

        for (int neighbor = 0; neighbor < isConnected.length; neighbor++) {
            if (isConnected[cur][neighbor] == 1 && ! visited[neighbor]) {
                dfsHelp(isConnected, visited, neighbor);
            }
        }

    }

}
